package com.example.bilibili.service.impl;

import com.example.bilibili.dao.BuserDao;
import com.example.bilibili.entity.Buser;
import com.example.bilibili.entity.GroupEntity;
import com.example.bilibili.service.BuserService;
import com.example.bilibili.utils.IDUtils;
import com.example.bilibili.utils.Node;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (Buser)表服务实现类
 *
 * @author makejava
 * @since 2022-08-16 20:29:29
 */
@Service("buserService")
public class BuserServiceImpl implements BuserService {
    @Resource
    private BuserDao buserDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Buser queryById(String id) {
        return this.buserDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param buser       筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<Buser> queryByPage(Buser buser, PageRequest pageRequest) {
        long total = this.buserDao.count(buser);
        String sort = '`' + pageRequest.getSort().toString().replace(':', '`');
        return new PageImpl<>(this.buserDao.queryAllByLimit(buser, pageRequest, sort), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param buser 实例对象
     * @return 实例对象
     */
    @Override
    public Buser insert(Buser buser) {
        buser.setId(IDUtils.getStringID());
        this.buserDao.insert(buser);
        return buser;
    }

    @Override
    public void insertList(List<Buser> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        buserDao.insertBatch(list);
    }

    /**
     * 修改数据
     *
     * @param buser 实例对象
     * @return 实例对象
     */
    @Override
    public Buser update(Buser buser) {
        this.buserDao.update(buser);
        return this.queryById(buser.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.buserDao.deleteById(id) > 0;
    }

    @Override
    public List<Node> group(GroupEntity group) {
        if (group.getRule() == null) {
            return buserDao.group(group.getKey(), group.getTable());
        }
        StringBuilder stringBuilder = new StringBuilder("select concat_ws(','");
        List<Node> list = new ArrayList<>(group.getRule().length - 1);
        for (int i = 0; i < group.getRule().length - 1; i++) {
            stringBuilder.append(String.format(",sum(if(`%s` >= %d and `%s` < %d, 1, 0))", group.getKey(), group.getRule()[i], group.getKey(), group.getRule()[i + 1]));
            Node node = new Node();
            node.setKey(String.format("%d 到 %d", group.getRule()[i], group.getRule()[i + 1]));
            list.add(node);
        }
        stringBuilder.append(") sValue from ").append(group.getTable());
        String[] ss = buserDao.groupNumber(stringBuilder.toString()).split(",");
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setSValue(ss[i]);
        }
        return list;
    }
}
